package com.one.group.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.one.group.admin.service.impl.SpCellServiceImpl;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.SpCellAddDto;
import com.one.group.model.dto.SpCellUpdateDto;
import com.one.group.model.entity.SpBuilding;
import com.one.group.model.entity.SpCell;
import com.one.group.model.entity.SpHouse;
import com.one.group.model.entity.SpUnit;
import com.one.group.result.Result;

import com.one.group.service.SpBuildingService;
import com.one.group.service.SpHouseService;
import com.one.group.service.SpUnitService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spCell")
@Tag(name = "小区接口")
public class SpCellController {

    @Autowired
    private SpCellServiceImpl spCellService;

    @Autowired
    private SpBuildingService spBuildingService;

    @Autowired
    private SpUnitService spUnitService;

    @Autowired
    private SpHouseService spHouseService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/findById")
    @Operation(summary = "通过id查询小区")
    public Result findById(Long id){
        SpCell spCell = spCellService.getById(id);
        if (spCell == null){
            return ResultUtil.error(ErrorCode.NOT_FOUND_ERROR, "小区不存在");
        }
        return ResultUtil.success(spCell);
    }
    @GetMapping("/findAll")
    @Operation(summary = "查询所有小区")
    public Result findAll(){
        return ResultUtil.success(spCellService.list());
    }
    @PostMapping("/create")
    @Operation(summary = "创建小区")
    public Result create(@RequestBody SpCellAddDto spCellAddDto){
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
        SpCell spCell = new SpCell();
        spCell.setName(spCellAddDto.getName());
        spCell.setExpirationTime(spCellAddDto.getExpirationTime());
        spCell.setAddress(spCellAddDto.getAddress());
        spCell.setBalance(spCellAddDto.getBalance());
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        rabbitTemplate.convertAndSend("gxa.direct","cellUpdate",tenantId);
        return ResultUtil.success(spCellService.save(spCell));

    }
    @PutMapping("/update")
    @Operation(summary = "更新小区")
    public Result update(@RequestBody SpCellUpdateDto spCellUpdateDto){
        if(spCellService.myUpdate(spCellUpdateDto)){
            return ResultUtil.success();
        }
        return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除小区")
    public Result delete(@PathVariable Long id){
        if(spCellService.myRemoveById(id)){
            return ResultUtil.success();
        }
        return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

    @GetMapping("/searchBuildingList/{cellId}")
    @Operation(summary = "查询小区下的楼栋列表")
    public Result searchBuildingList(@PathVariable Long cellId){
        if (ObjectUtil.isEmpty(cellId)){
            // 小区id不能为空
            return ResultUtil.error(ErrorCode.PARAMS_ERROR);
        }
        List<SpBuilding> spBuildingList = spBuildingService.searchBuildingList(cellId);
        // 查询该小区的所有楼栋
        return ResultUtil.success(spBuildingList);
    }

    @GetMapping("/searchUnitList/{buildingId}")
    @Operation(summary = "查询小区下的单元列表")
    public Result searchUnitList(@PathVariable Long buildingId){
        if (ObjectUtil.isEmpty(buildingId)){
            // 楼栋id不能为空
            return ResultUtil.error(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper lambdaQueryWrapper= QueryWrapper.create().from(SpUnit.class);
        lambdaQueryWrapper.eq(SpUnit::getBuildingId,buildingId);
        List<SpUnit> spUnitList = spUnitService.list(lambdaQueryWrapper);
        // 查询该小区的所有楼栋
        return ResultUtil.success(spUnitList);
    }

    @GetMapping("/searchHouseList/{unitId}")
    @Operation(summary = "查询小区下的房屋列表")
    public Result searchHouseList(@PathVariable Long unitId){
        if (ObjectUtil.isEmpty(unitId)){
            // 楼栋id不能为空
            return ResultUtil.error(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper lambdaQueryWrapper= QueryWrapper.create().from(SpHouse.class);
        lambdaQueryWrapper.eq(SpHouse::getUnitId,unitId);
        List<SpHouse> spHouseList = spHouseService.list(lambdaQueryWrapper);
        // 查询该小区的所有楼栋
        return ResultUtil.success(spHouseList);
    }

}
